//
//  MyZoneViewController.m
//  JiuLe
//
//  Created by xcq on 15/11/2.
//  Copyright © 2015年 Fine. All rights reserved.
//

#import "JlUserManager.h"
#import "MyZoneViewController.h"

#define kLoginUrl @"http://112.124.35.198/usercenter/index.php/User/Login/dologin"
#define kMyZoneUrl @"http://112.124.35.198/usercenter/index.php/User/Index/index"

@interface MyZoneViewController  ()
@property (assign, nonatomic) BOOL isLoad;
@property (assign, nonatomic) BOOL isSuccess;
@property (assign, nonatomic) BOOL isLoadOtherPage;
@end

@implementation MyZoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _webView.delegate = self;
    _isLoad = NO;
    _isLoadOtherPage=NO;
    _isSuccess = NO;
    _webView.scalesPageToFit = YES;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (_isSuccess == NO) {
        [self loadRequest];
    }
}

- (void)loadRequest {
    JlUserManager *manager = [JlUserManager shareInstance];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:kLoginUrl] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.f];
    request.HTTPMethod = @"POST";
    NSString *str = [NSString stringWithFormat:@"username=%@&password=%@", manager.userInfo.username,[[NSUserDefaults standardUserDefaults] objectForKey:UserDataPassWord]];
//    request.HTTPBody =  [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];;
    request.HTTPBody = [str dataUsingEncoding:NSUTF8StringEncoding];
    [_webView loadRequest:request];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    if (_isLoadOtherPage) {
        [SVProgressHUD dismiss];
        return ;
    }
    if (!_isLoad && [[webView.request.URL absoluteString] isEqualToString:kMyZoneUrl]) {
        UIView *view = [self.view viewWithTag:500];
        if (view) {
            [view removeFromSuperview];
        }
        [SVProgressHUD dismiss];
        return;
    }
    [_webView loadRequest:[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:kMyZoneUrl] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.f]];
}


- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:
(UIWebViewNavigationType)navigationType {
    [SVProgressHUD showWithStatus:@"加载中.."];
    if ([request.URL.absoluteString  isEqualToString:kLoginUrl]) {
        UIView *view = [UIView new];
        view.frame = _webView.frame;
        view.backgroundColor = [UIColor whiteColor];
        view.tag = 500;
        [self.view addSubview:view];
        [self.view bringSubviewToFront:_webView];
        _isLoad = YES;
    } else if ([request.URL.absoluteString  isEqualToString:kMyZoneUrl]) {
        _isLoad = NO;
        _isSuccess = YES;
    } else {
        _isLoadOtherPage = YES;
        _isSuccess = YES;
    }
    return  YES;
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    _isSuccess = NO;
    _isLoadOtherPage = NO;
    [SVProgressHUD dismiss];
    [SVProgressHUD showErrorWithStatus:@"请求失败.."];
}

@end
